package com.zlb.app.utils;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.zlb.app.common.exception.BusinessException;
import com.zlb.app.common.properties.TianApiProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description
 * @Author: zQY000568
 * @Date: 2024-09-06
 */
@Component
public class TianUtil {

    private static TianApiProperties tianApiProperties;

    @Autowired
    public void setTianApiProperties(TianApiProperties tianApiProperties) {
        TianUtil.tianApiProperties = tianApiProperties;
    }


    public static JSONObject get(String path, Map<String, Object> params) {
        if (params == null) {
            params = new HashMap<>();
        }
        params.put("key", tianApiProperties.getKey());
        String result = HttpUtil.get(tianApiProperties.getBaseUrl() + path, params);
        return JSONObject.parseObject(result);
    }

    public static JSONObject doGet(String path, Map<String, Object> params) {
        JSONObject json = get(path, params);
        if (json.getInteger("code") != 200) {
            throw new BusinessException(json.getString("msg"));
        }
        return json;
    }


}
